import 'dart:math';

import 'package:enterprise_v6_app/app/components/signature/signature_controller.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:syncfusion_flutter_signaturepad/signaturepad.dart';

/// 签字板
class SignatureWidget extends StatelessWidget {
  SignatureWidget({super.key});

  final controller = Get.put(SignatureController());

  ///翻转90度
  Widget _getReversalView({required Widget child}) {
    return Transform.rotate(
      angle: pi / 2, // 翻转90度
      child: child,
    );
  }

  ///按钮样式
  Widget _getButtonView(
      {required String title,
      required VoidCallback onPressed,
      Color? backgroundColor}) {
    return Container(
      height: 100.w,
      width: 42.w,
      decoration: BoxDecoration(
          color: backgroundColor,
          border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
          borderRadius: BorderRadius.all(Radius.circular(8.w))),
      alignment: Alignment.center,
      child: _getReversalView(
          child: Text(title,
              textDirection: TextDirection.ltr,
              style: TextStyle(
                color: Colors.white, // 设置文本颜色
                fontSize: 16.sp, // 设置字体大小
              ))),
    ).clickOnce(() {
      onPressed();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: [
          Container(
              width: 75.w,
              color: const Color(0xffF5F5F6),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  SizedBox(height: 60.w),
                  _getButtonView(
                      title: '返回',
                      onPressed: controller.back,
                      backgroundColor: const Color(0xff5C6DFF)),
                  const Spacer(),
                  _getButtonView(
                      title: '清除',
                      onPressed: controller.clear,
                      backgroundColor: const Color(0xff74B5FF)),
                  SizedBox(height: 20.w),
                  _getButtonView(
                      title: '保存',
                      onPressed: controller.save,
                      backgroundColor: const Color(0xffFF9900)),
                  SizedBox(height: 40.w),
                ],
              )),
          Expanded(
            child: Stack(
              children: [
                Center(
                  child: _getReversalView(
                      child: ButtonTheme(
                    minWidth: 200.w,
                    height: 50.w,
                    child: Text(
                      '此处签名',
                      style: TextStyle(
                        color: Colors.grey[300],
                        fontSize: 60,
                      ),
                    ),
                  )),
                ),
                Obx(() {
                  return SfSignaturePad(
                    key: controller.signaturePadKey,
                    minimumStrokeWidth: 2,
                    maximumStrokeWidth: 7,
                    strokeColor: Colors.black,
                    backgroundColor: controller.isShowHint.value
                        ? Colors.transparent
                        : Colors.white,
                    onDrawEnd: () {
                      controller.isShowHint.value = false;
                    },
                  );
                }),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
